package com.note.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName FastJsonParse
 * @Description TODO
 * @Author QiBin
 * @Date 2020/6/1017:50
 * @Version 1.0
 **/

public class FastJsonParse {
    public static void main(String[] args) {
        //模拟数据
        String str ="复杂的string,如下";
//                "{
//            \"generalWorker\":[{\"id\":\"zjqf001\",\"projectId\":\"1\",\"workType\":\"3\"},{\"id\":\"zjqf002\",\"projectId\":\"1\",\"workType\":\"3\"},{\"id\":\"zjqf003\",\"projectId\":\"1\",\"workType\":\"3\"},{\"id\":\"zjqf004\",\"projectId\":\"1\",\"workType\":\"3\"}],
//            \"projectMember\":[{\"displayOrdinal\":0,\"gender\":0,\"idCardNumber\":\"123\",\"idCardType\":\"身份证\"},{\"displayOrdinal\":0,\"gender\":1,\"idCardNumber\":\"456\",\"idCardType\":\"居住证\"},{\"displayOrdinal\":0,\"gender\":0,\"idCardNumber\":\"789\",\"idCardType\":\"健康证\"}],
//            \"skilledWorker\":[{\"cardNumber\":\"WL\",\"cardStatus\":\"9\",\"empno\":\"003\"},{\"cardNumber\":\"ER\",\"cardStatus\":\"7\",\"empno\":\"004\"},{\"cardNumber\":\"FG\",\"cardStatus\":\"8\",\"empno\":\"005\"}]}";

        //String str = "{\"generalWorker\":2,\"skillWorker\":3}";

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        JSONObject jsonObject = JSON.parseObject(str);

        //判读是否符合数据格式
        if(str.indexOf("[") == -1){
            Map<String,Object> newMap = jsonObject;
            list.add(newMap);
        }else{
            //将[]内容替换成A，模拟成数据格式为：{"generalWorker":"A","projectMember":"A","skilledWorker":"A"}
            int count = 0;
            int num = 0;
            while(count != -1 && num!=-1){
                count = str.indexOf("[",count+1);
                num = str.indexOf("]",count+1);
                if(count !=-1 && num!=-1){
                    str = str.replace(str.substring(count,num+1),"\"A\"");
                }
            }

            HashMap<String, Object> map = new HashMap<>();
            HashMap dataMap = JSON.parseObject(str, HashMap.class);
            //dataMap={"generalWorker":"A","projectMember":"A","skilledWorker":"A"}
            for (Object obj : dataMap.keySet()){//遍历json的根节点
                //转成jsonArray
                JSONArray jsonArray = (JSONArray)jsonObject.getJSONArray(obj.toString());
                map.put(obj.toString(),jsonArray);
            }
            list.add(map);
        }
        System.out.println(list);
    }
/*
输出结果：
[{
generalWorker=[{"workType":"3","id":"zjqf001","projectId":"1"},{"workType":"3","id":"zjqf002","projectId":"1"},{"workType":"3","id":"zjqf003","projectId":"1"},{"workType":"3","id":"zjqf004","projectId":"1"}],
projectMember=[{"idCardType":"身份证","displayOrdinal":0,"gender":0,"idCardNumber":"123"},{"idCardType":"居住证","displayOrdinal":0,"gender":1,"idCardNumber":"456"},{"idCardType":"健康证","displayOrdinal":0,"gender":0,"idCardNumber":"789"}],
skilledWorker=[{"empno":"003","cardNumber":"WL","cardStatus":"9"},{"empno":"004","cardNumber":"ER","cardStatus":"7"},{"empno":"005","cardNumber":"FG","cardStatus":"8"}]
}]
————————————————
版权声明：本文为CSDN博主「Zz罗伯特」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
原文链接：https://blog.csdn.net/zz18435842675/article/details/98059570
 */
}
